package edu.umassd.hciproject;

import org.json.JSONArray;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {
	Button bFindProducts, bMyLists, bMyPlaces;
	ImageView ivTarget, ivStopShop, ivWalmart;
	static final String[] bFindProductsOptions = {"My Lists", "New List"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bFindProducts = (Button) findViewById(R.id.bFindProducts);
		bFindProducts.setOnClickListener(this);

		bMyLists = (Button) findViewById(R.id.bMyLists);
		bMyLists.setOnClickListener(this);
		bMyPlaces = (Button) findViewById(R.id.bMyPlaces);
		bMyPlaces.setOnClickListener(this);

		ivTarget = (ImageView) findViewById(R.id.ivTarget);
		ivTarget.setOnClickListener(this);
		ivStopShop = (ImageView) findViewById(R.id.ivStopShop);
		ivStopShop.setOnClickListener(this);
		ivWalmart = (ImageView) findViewById(R.id.ivWalmart);
		ivWalmart.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bFindProducts:
			AlertDialog.Builder builder = new AlertDialog.Builder(this).setItems(bFindProductsOptions, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					if (which == 0) {
						startActivity(new Intent(MainActivity.this, MyListsActivity.class));
					} else if (which == 1) {
						startActivity(new Intent(MainActivity.this, NewListActivity.class));
					}
				}
			});
			builder.setTitle("Find products from...");
			builder.show();
			break;
		case R.id.bMyLists:
			startActivity(new Intent(this, MyListsActivity.class));
			break;
		case R.id.bMyPlaces:
			startActivity(new Intent(this, MyPlacesActivity.class));
			break;
		case R.id.ivTarget:
			try {
				startActivity(new Intent(Intent.ACTION_VIEW,
						Uri.parse("http://www.target.com")));
			} catch (Exception e) {
				Toast t = Toast.makeText(this, "Unable to open browser",
						Toast.LENGTH_SHORT);
				t.show();
			}
			break;
		case R.id.ivStopShop:
			try {
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_VIEW);
				intent.addCategory(Intent.CATEGORY_BROWSABLE);
				intent.setData(Uri.parse("http://www.stopandshop.com"));
				startActivity(intent);
			} catch (Exception e) {
				Toast t = Toast.makeText(this, "Unable to open browser",
						Toast.LENGTH_SHORT);
				t.show();
			}
			break;
		case R.id.ivWalmart:
			try {
				startActivity(new Intent(Intent.ACTION_VIEW,
						Uri.parse("http://www.walmart.com")));
			} catch (Exception e) {
				Toast t = Toast.makeText(this, "Unable to open browser",
						Toast.LENGTH_SHORT);
				t.show();
			}
			break;
		default:
			return;
		}
	}

}
